package lgkj.meicqt20.ui;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import lgkj.meicqt20.R;
import lgkj.meicqt20.adapter.GalleryAdapter;
import lgkj.meicqt20.adapter.VideoListAdapter;
import lgkj.meicqt20.adapter.VideoTypeAdapter;
import lgkj.meicqt20.bean.MessageEvent;
import lgkj.meicqt20.bean.VideoTypeEntity;
import lgkj.meicqt20.bean.VideosEntity;
import lgkj.meicqt20.utils.AllKindsOfConstant;
import lgkj.meicqt20.utils.HttpUtils;
import lgkj.meicqt20.view.BannerViewpager;

/**
 * Created by Administrator on 2019/1/18 0018.
 */

public class VideosActivity extends BaseActivity {
    private int ordertype = 0;
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.video_little_banner)
    BannerViewpager videoLittleBanner;
    @BindView(R.id.lv_left_type)
    ListView lvLeftType;
    @BindView(R.id.rv_type_top)
    RecyclerView rvTypeTop;
    @BindView(R.id.tv_data)
    TextView tvData;
    @BindView(R.id.gv_movie)
    GridView gvMovie;
    @BindView(R.id.ll_video_type)
    LinearLayout llVideoType;
    private VideoTypeAdapter adapter1;
    private GalleryAdapter adapter2;
    private VideoListAdapter adapter3;
    public static boolean isOrderDeal = true;

    @Override
    public int setContentView() {
        return R.layout.activity_videos;
    }

    @Override
    public void init() {
        activitiesList.add(this);
        HttpUtils.getQRCode(VideosActivity.this, ivCode);
        adapter1 = new VideoTypeAdapter(VideosActivity.this);
        lvLeftType.setAdapter(adapter1);
        adapter2 = new GalleryAdapter(VideosActivity.this);
        adapter3 = new VideoListAdapter(VideosActivity.this);
        adapter3.setShow(true);
        gvMovie.setAdapter(adapter3);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(VideosActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvTypeTop.setLayoutManager(linearLayoutManager);
        rvTypeTop.setAdapter(adapter2);
    }

    @Override
    public void onResume() {
        super.onResume();
        displayDifferen();
        EventBus.getDefault().register(this);
        Log.d(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        Log.i(TAG, "socket: " + messageEvent.toString());
        if ("control".equals(messageEvent.getTagName())) {
            fragmentDealOrder(messageEvent.getMessage());
        }
    }

    private void fragmentDealOrder(String order) {
        Log.d(TAG, "ordertype:" + ordertype + ",namePositionX:" + namePositionX + ",oder:" + order + ",,,,=======" + adapter3.isShow());
        if (ordertype == 1) {
            if (adapter3.getCount() > 0) {
                movieDealOrder(order);
            }
        } else {
            leftdealOrder(order);
        }
        Log.d(TAG, "ordertype:" + ordertype + ",namePositionX:" + namePositionX + ",oder:" + order + ",isShow:" + adapter3.isShow());
    }

    @Override
    public void initData() {
        initAdapter1();
    }


    private int leftPositon = 0;
    private int rightPositon = -1;
    private int namePositionX = -1;

    /**
     * 电影
     *
     * @param order 1
     */
    private void movieDealOrder(String order) {
        Log.i(TAG, "movieDealOrder: " + order + ",isOrderDeal:" + isOrderDeal);
        if (!isOrderDeal) {
            return;
        }
        switch (order) {
            case "up":
                if (namePositionX < 3) {
                } else {
                    namePositionX = namePositionX - 3;
                }
                break;
            case "down":
                if (namePositionX < adapter3.getCount()) {
                    if (namePositionX <= -1) {
                        namePositionX = 0;
                    } else {
                        if ((adapter3.getCount() - namePositionX) > 3) {
//                        namePositionX = adapter3.getCount() - 1;
                            namePositionX = namePositionX + 3;
                        }
                    }
                    adapter3.setPosition(namePositionX);
                }
                break;
            case "right":
                if ((namePositionX + 1) < adapter3.getCount()) {
                    adapter3.setShow(true);
                    if (namePositionX % 3 != 2) {
                        ++namePositionX;
                        adapter3.setPosition(namePositionX);
                    }
                }
                break;
            case "left":
                if (namePositionX % 3 == 0) {
                    ordertype = 0;
                    if (namePositionX >= 0) {
                        adapter3.setShow(false);
                    } else {
                        namePositionX = -1;
                    }
                } else {
                    if (namePositionX > 0) {
                        --namePositionX;
                    }
                }
                break;
            case "sure":
                if (namePositionX > -1 && namePositionX <= adapter3.getCount()) {
                    String video_id = adapter3.getItem(namePositionX).getVideo_id();
                    Intent intent = new Intent(VideosActivity.this, VideosDetailsActivity.class);
                    intent.putExtra("video_id", video_id);
                    startActivity(intent);
                }
                break;
            case "back":
                finish();
                break;
        }
        adapter3.setPosition(namePositionX);
        gvMovie.smoothScrollToPositionFromTop(namePositionX, 0);
        adapter3.notifyDataSetChanged();
    }

    /**
     * 类型    0
     *
     * @param order
     */
    private void leftdealOrder(String order) {
        switch (order) {
            case "up":
                if (leftPositon < adapter1.getCount() && leftPositon > 0) {
                    leftPositon--;
                    adapter1.setPosition(leftPositon);
                    rightPositon = -1;
                    List<VideoTypeEntity.DataBean.TypeListBean> typeListBeen = mVideoTypes.getData().get(leftPositon).getType_list();
                    if (typeListBeen != null) {
                        adapter2.setData(typeListBeen);
                    }
                }
                lvLeftType.smoothScrollToPosition(leftPositon);
                break;
            case "down":
                if (rightPositon > -1) {
                    ordertype = 1;
                } else {
                    if ((leftPositon + 1) < adapter1.getCount()) {
                        lvLeftType.smoothScrollToPositionFromTop(leftPositon, 0, 40);
                        leftPositon++;
                        adapter1.setPosition(leftPositon);
                    }
                    rightPositon = -1;
                    try {
                        if (mVideoTypes.getData().get(leftPositon).getType_list() != null) {
                            adapter2.setData(mVideoTypes.getData().get(leftPositon).getType_list());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case "left":
                break;
            case "right":
                if (adapter3.getCount() > 1) {
                    ordertype = 1;
                    if (namePositionX == -1) {
                        namePositionX = 0;
                    }
                    adapter3.setShow(true);
                }
                break;
            case "back":
                finish();
                break;
        }
        adapter3.setPosition(namePositionX);
        if (!"right".equals(order)) {
            namePositionX = -1;
            if (adapter2.getItem(0) != null && adapter2.getItem(0).getParent_id() != null) {
                getVideoList(adapter2.getItem(0).getParent_id());
            }

            gvMovie.smoothScrollToPositionFromTop(namePositionX, 0);
        }
        adapter1.notifyDataSetChanged();
        adapter3.notifyDataSetChanged();
        if (adapter2.getItemCount() > 0) {
        } else {
            adapter3.setData(null);
            adapter3.notifyDataSetChanged();
            tvData.setText("暂无更多数据");
            tvData.setVisibility(View.VISIBLE);
        }
    }


    private void initAdapter1() {
        ordertype = 0;
        leftPositon = 0;
        rightPositon = -1;
        List<VideoTypeEntity.DataBean.TypeBean> initAdapterList1 = new ArrayList<>();
        List<VideoTypeEntity.DataBean.TypeListBean> initAdapterList2 = new ArrayList<>();
        getTypeLeftId(initAdapterList1, initAdapterList2);
    }

    /**
     * 获取电影类型
     *
     * @param initAdapterList1
     * @param initAdapterList2
     */
    private VideoTypeEntity mVideoTypes;

    private void getTypeLeftId(final List<VideoTypeEntity.DataBean.TypeBean> initAdapterList1, final List<VideoTypeEntity.DataBean.TypeListBean> initAdapterList2) {
        HttpUtils.getHttp(null, AllKindsOfConstant.getvideoType, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("status") == 1) {
                    Gson mGson = new Gson();
                    mVideoTypes = mGson.fromJson(String.valueOf(jsonObject), VideoTypeEntity.class);
                    adapter1.setData(mVideoTypes.getData());
                    if (mVideoTypes.getData().get(0) != null) {
                        adapter2.setData(mVideoTypes.getData().get(0).getType_list());
                        if (mVideoTypes.getData().get(0).getType_list().get(0) != null) {
                            getVideoList(mVideoTypes.getData().get(0).getType_list().get(0).getParent_id());
                        }
                    }
                    adapter1.setPosition(leftPositon);
                    adapter2.setPositon(rightPositon);
                    adapter1.notifyDataSetChanged();
                    adapter2.notifyDataSetChanged();
                } else {
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    private VideosEntity mVideosEntity;

    /**
     * 电影列表
     *
     * @param video_type_id
     */

    protected void getVideoList(String video_type_id) {
        tvData.setText("正在加载数据...");
        tvData.setVisibility(View.VISIBLE);
        final Map<String, String> params = new HashMap<String, String>();
        params.put("video_type_id", video_type_id);
        HttpUtils.getHttp(params, AllKindsOfConstant.getVideoInfo, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("status") == 1) {
                    Gson mGson = new Gson();
                    mVideosEntity = mGson.fromJson(String.valueOf(jsonObject), VideosEntity.class);
                    adapter3.setData(mVideosEntity.getData());
                    adapter3.setPosition(-1);
                    adapter3.notifyDataSetChanged();
                    tvData.setVisibility(View.GONE);
                } else {
                    tvData.setText("暂无更多数据");
                    tvData.setVisibility(View.VISIBLE);
                    adapter3.setData(null);
                    adapter3.notifyDataSetChanged();

                }

            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }
}
